package com.pingao.mp.config;

import com.pingao.mp.handle.AuthenticationInterceptor;
import com.pingao.mp.handle.LogHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MpWebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private MpConfig mpConfig;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");//增加swagger的支持
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");//webjars
        registry.addResourceHandler("/" + mpConfig.getFilePath() + "/**").addResourceLocations("file:" + "./" + mpConfig.getFilePath() + "/");//文件上传映射
    }

    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }

    @Bean
    public LogHandlerInterceptor logHandlerInterceptor() {
        return new LogHandlerInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor())
                .addPathPatterns("/api/**");//拦截/api/**
        //registry.addInterceptor(logHandlerInterceptor()).excludePathPatterns("/sys/log/list").addPathPatterns("/sys/**", "/fls/**");
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                //.allowedOrigins("http://192.168.89.89")
                //rest集中请求方式
                .allowedMethods("GET", "POST", "DELETE")
                .allowCredentials(false).maxAge(3600);
    }
}
